package server;
// NetworkInputThread.java
// Used by the server
// Sits in a while loop blocking until a client makes a request
// which is sent to addRequest() in the UpdateThread passed into the constructor
// Runs in a separate thread so blocking doesn't interrupt the application
// Uses the ID to distinguish itself.

import java.io.ObjectInputStream;
import java.net.Socket;
import java.net.SocketException;

import client.ClientRequest;

class NetworkInputThread implements Runnable
{
	private Socket socket;
	private UpdateThread updateThread;  // The "Listener" for this class
	private int ID = -1;

	public NetworkInputThread(int id, Socket s, UpdateThread u)
	{
		ID = id;
		socket = s;
		updateThread = u;
	}

	public void run()
	{
		try
		{	// Create input stream
			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
			while(true)
			{	// Send request to UpdateThread object
				updateThread.addRequest((ClientRequest)in.readObject(), ID);
			}
		}
		catch(SocketException e)
		{
			//e.printStackTrace();
			System.out.println("Input stream for client " + ID + " closed.");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}